//
//  TravelDiaryViewController.m
//  MonkeyTravel
//
//  Created by Wan on 16/1/8.
//  Copyright © 2016年 WanWanSui. All rights reserved.
//

#import "TravelDiaryViewController.h"
#import "TravelDiaryCollectionViewCell.h"

@interface TravelDiaryViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation TravelDiaryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor lightGrayColor];
    _allModelArr = [NSMutableArray array];
    [self creatView];
    [self loadData];
}

-(void)creatView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置item大小
    flowLayout.itemSize = CGSizeMake(WIDTH - 20, 170*XHIGHT);
    //item列间距
    flowLayout.minimumInteritemSpacing = 10;
    //item行间距
    //flowLayout.minimumLineSpacing = 0;
    //设置item上，左，下，右 边界距离
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    //头部区域大小
    flowLayout.headerReferenceSize = CGSizeMake(0, 0);
    //底部区域大小
    flowLayout.footerReferenceSize = CGSizeMake(0, 0);
    //滑动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //创建 UICollectionView
    _myCollectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    
    _myCollectionView.frame = CGRectMake(0, 0, 0, 0);
    _myCollectionView.tag = 1000;
    //    self.collectionView.backgroundColor = [UIColor whiteColor];
    _myCollectionView.delegate = self;
    _myCollectionView.dataSource = self;
    [self.view addSubview:_myCollectionView];
    //注册cell,标示符必须与cell的一样
    [_myCollectionView registerClass:[ TravelDiaryCollectionViewCell class] forCellWithReuseIdentifier:@"TravelDiaryCollectionViewCell"];
    //[self.myCollectionView registerClass:[<#UICollectionViewCell#> class] forCellWithReuseIdentifier:@"FitFood"];
    //注册表头和底部视图
    [_myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    [_myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];
}

-(void)loadData
{
    [SSNetWorkTool getWithUrl:@"http://chanyouji.com/api/trips/featured.json?page=1" withParameter:nil withHttpHeader:nil withResponseType:ResponseTypeJSON withSuccess:^(id result) {
        NSMutableArray *array = result;
        for (NSMutableDictionary *dic in array) {
            TravelDiary *diary = [[TravelDiary alloc] initWithDic:dic];
            [_allModelArr addObject:diary];
        }
        [_myCollectionView reloadData];
    } withFail:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSLog(@"%ld",_allModelArr.count);
    return _allModelArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TravelDiaryCollectionViewCell *TDCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TravelDiaryCollectionViewCell" forIndexPath:indexPath];
    return TDCell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
